package com.wesley.growth.itext7.chapter1;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.io.resource.ResourceUtil;
import com.itextpdf.io.font.constants.StandardFonts;
import com.itextpdf.io.image.ImageDataFactory;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.geom.PageSize;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.*;
import com.itextpdf.layout.properties.UnitValue;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.charset.StandardCharsets;
import java.util.StringTokenizer;

/**
 * HelloWorld
 *
 * @author WangPanYong
 * @since 2023/03/29 17:49
 */
public class HelloWorld {
    public static void main(String[] args) throws IOException {
//        hello();
        table();
    }

    /**
     * hello
     * listItem 和 Image 演示
     * @author WangPanYong
     * @since 2023/03/30 15:14
     **/
    private static void hello() throws IOException{
        File dir = new File("temp");
        File pdfFile = FileUtil.createTempFile("hello_", ".pdf", dir, true);
        // PdfWriter是一个可以写PDF文件的对象，它不需要了解它要写的pdf的实际内容是什么
        PdfWriter writer = new PdfWriter(pdfFile);
        PdfDocument pdf = new PdfDocument(writer);
        Document document = new Document(pdf);
        document.add(new Paragraph("Hello World!"));
        addListItem(document);
        addImage(document);
        document.close();
        System.out.println("pdfFile.getName() = " + pdfFile.getName());
    }

    private static void addListItem(Document document) throws IOException {
        // Create a PdfFont
        PdfFont font = PdfFontFactory.createFont(StandardFonts.TIMES_ROMAN);
        // Add a Paragraph
        document.add(new Paragraph("iText is:").setFont(font));
        // Create a List
        List list = new List()
                .setSymbolIndent(12)
                .setListSymbol("\u2022")
                .setFont(font);
        // Add ListItem objects
        list.add(new ListItem("Never gonna give you up"))
            .add(new ListItem("Never gonna let you down"))
            .add(new ListItem("Never gonna run around and desert you"))
            .add(new ListItem("Never gonna make you cry"))
            .add(new ListItem("Never gonna say goodbye"))
            .add(new ListItem("Never gonna tell a lie and hurt you"));
        // Add the list
        document.add(list);
    }

    private static void addImage(Document document) throws MalformedURLException {
        File image1 = new File("D:\\Desktop\\Temp\\图片\\test.png");
        File image2 = new File("D:\\Desktop\\Temp\\图片\\test3.png");
        Image fox = new Image(ImageDataFactory.create(image1.getAbsolutePath()));
        fox.setWidth(120);
        fox.setHeight(80);
        Image dog = new Image(ImageDataFactory.create(image2.getAbsolutePath()));
        dog.setMaxWidth(120);
        dog.setMaxHeight(80);
        Paragraph p = new Paragraph("The quick brown ")
                .add(fox)
                .add(" jumps over the lazy ")
                .add(dog);
        document.add(p);
    }

    /**
     * 表格演示
     */
    private static void table() throws IOException{
        File dir = new File("temp");
        File pdfFile = FileUtil.createTempFile("table_", ".pdf", dir, true);
        PdfWriter writer = new PdfWriter(pdfFile);
        PdfDocument pdf = new PdfDocument(writer);
        // A4纸旋转
        Document document = new Document(pdf, PageSize.A4.rotate());
        document.setMargins(20, 20, 20, 20);

        PdfFont font = PdfFontFactory.createFont(StandardFonts.HELVETICA);
        PdfFont bold = PdfFontFactory.createFont(StandardFonts.HELVETICA_BOLD);
        Table table = new Table(new float[]{4, 1, 3, 4, 3, 3, 3, 3, 1});
        table.setWidth(UnitValue.createPercentValue(100));

        BufferedReader br = IoUtil.getReader(ResourceUtil.getStream("usa.csv"), StandardCharsets.UTF_8);
        String line = br.readLine();
        process(table, line, bold, true);
        while ((line = br.readLine()) != null) {
            process(table, line, font, false);
        }
        br.close();
        document.add(table);
        document.close();
    }

    private static void process(Table table, String line, PdfFont font, boolean isHeader) {
        StringTokenizer tokenizer = new StringTokenizer(line, ";");
        while (tokenizer.hasMoreTokens()) {
            if (isHeader) {
                table.addHeaderCell(
                        new Cell().add(
                                new Paragraph(tokenizer.nextToken()).setFont(font)));
            } else {
                table.addCell(
                        new Cell().add(
                                new Paragraph(tokenizer.nextToken()).setFont(font)));
            }
        }
    }

}
